package edu.scut.course_management.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.scut.course_management.dto.BuildingIdDTO;
import edu.scut.course_management.dto.BuildingIdListDTO;
import edu.scut.course_management.dto.GetCourseTypeDTO;
import edu.scut.course_management.dto.RoomNumberDTO;
import edu.scut.course_management.dto.YearDTO;
import edu.scut.course_management.po.AcademicTitle;
import edu.scut.course_management.po.CourseType;
import edu.scut.course_management.po.EnrollmentYear;
import edu.scut.course_management.result.JsonResult;
import edu.scut.course_management.result.JsonResultFactory;
import edu.scut.course_management.result.ResultCode;
import edu.scut.course_management.service.InfomationService;
import edu.scut.course_management.util.ParamCheckingUtil;

/**
 * 信息管理模块
 * 
 * @author sherry
 */

@RestController
@RequestMapping("/Info")
public class InfoController {
    @Autowired
    InfomationService infomationService;

    /**
     * 返回院系列表
     */
    @GetMapping("/Department")
    public JsonResult getAllDepartments () {
        JsonResult result = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        result = jsonResultFactory.buildSuccessResult(infomationService.getAllDepartments());
        return result;
    }

    /**
     * 返回入学年份列表
     */
    @GetMapping("/EnrollmentYear")
    public JsonResult getAllEnrollementYears () {
        JsonResult result = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        
        List<EnrollmentYear> enrollmentList = infomationService.getAllEnrollmentYears();
        String[] enrollmentYears = new String[enrollmentList.size()];
        
        for (int i = 0; i <  enrollmentList.size(); i++) {
			enrollmentYears[i] =  enrollmentList.get(i).getEnrollmentYear();
        }

        Map <String, String[]> map = new HashMap<>();
        map.put("enrollmentYears", enrollmentYears);

        result = jsonResultFactory.buildSuccessResult(map);
        return result;
    }

    /**
     * 返回院系专业列表
     */
    @PostMapping("/Department/Major")
    public JsonResult getDepartmentMajors (@RequestBody Map<String, Integer> params) {
        JsonResult result = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();

        if (!ParamCheckingUtil.paramsNotNull(params, "departmentId")) {
            result = jsonResultFactory.buildFailResult(ResultCode.PARAM_NOT_COMPLETE);
        } 
        else if (!infomationService.isExistDepartment(params.get("departmentId"))) {
            result = jsonResultFactory.buildFailResult(ResultCode.PARAM_ERROR, "院系不存在");
        }
        else {
            Integer departmentId = params.get("departmentId");
            result = jsonResultFactory.buildSuccessResult(infomationService.getDepartmentMajors(departmentId));
        }

        return result;
    }

    /**
     * 返回院系班级列表
     */
    @PostMapping("/Department/Major/Class")
    public JsonResult getMajorClasses (@RequestBody Map<String, Object> params) {
        JsonResult result = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();

        if (!ParamCheckingUtil.paramsNotNull(params, "majorId")) {
            result = jsonResultFactory.buildFailResult(ResultCode.PARAM_NOT_COMPLETE);
        } 
        else if (!infomationService.isExistMajor((Integer)params.get("majorId"))) {
            result = jsonResultFactory.buildFailResult(ResultCode.PARAM_ERROR, "专业不存在");
        } 
        else {
            Integer majorId = (Integer)params.get("majorId");
            String enrollmentYear = (String)params.get("enrollmentYear");
            result = jsonResultFactory.buildSuccessResult(infomationService.getMajorClasses(majorId, enrollmentYear));
        }

        return result;
    }

    /**
     * 返回职称列表
     */
    @GetMapping("/AcademicTitle")
    public JsonResult getAllAcademicTitles () {
        JsonResult result = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();

        List<AcademicTitle> academicTitleList = infomationService.getAllAcademicTitles();
        String[] academicTitles = new String[academicTitleList.size()];
        
        for (int i = 0; i <  academicTitleList.size(); i++) {
			academicTitles[i] =  academicTitleList.get(i).getAcademicTitle();
        }

        Map <String, String[]> map = new HashMap<>();
        map.put("academicTitles", academicTitles);

        result = jsonResultFactory.buildSuccessResult(map);
        return result;
    }
    
    /**
     * 返回楼栋列表
     * URL: http://localhost:8080/Info/Building
     * GET
     */
    @RequestMapping("/Building")
    public JsonResult getAllBuildingId(){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        List<String> buildings = infomationService.getAllBuildingIds();
        //对数据进行封装
        BuildingIdListDTO buildingIdListDTO = new BuildingIdListDTO();
        buildingIdListDTO.setBuildings(buildings);

        jsonResult = jsonResultFactory.buildSuccessResult(buildingIdListDTO); 
        return jsonResult;
    }

    /**
     * 返回教室列表
     * URL: http://localhost:8080/Info/Building/Classroom
     * POST
     */
    @PostMapping("/Building/Classroom")
    public JsonResult getRoomNumber(@RequestBody BuildingIdDTO buildingIdDTO){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();

        //获取需要的数据
        String buildingId = buildingIdDTO.getBuildingId();
        if(buildingId.isEmpty()){//判断参数是否为空
            jsonResult = jsonResultFactory.buildFailResult(ResultCode.PARAM_IS_BLANK);
        }
        else{
            RoomNumberDTO roomNumberDTO = new RoomNumberDTO();
            roomNumberDTO.setRoomNumbers(infomationService.getAllRoomNumbers(buildingId));
            jsonResult = jsonResultFactory.buildSuccessResult(roomNumberDTO);
        }

        return jsonResult;
    }

    /**
     * 返回课程类别列表
     * URL: http://localhost:8080/Info/CourseType
     * GET
     */
    @RequestMapping("/CourseType")
    public JsonResult getCourseType(){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        GetCourseTypeDTO getCourseTypeDTO = infomationService.getAllCourseType();
        jsonResult = jsonResultFactory.buildSuccessResult(getCourseTypeDTO);
        return jsonResult;
    }

    /**
     * 返回学年列表
     * URL: http://localhost:8080/Info/Years
     * GET
     */
    @RequestMapping("/Years")
    public JsonResult getYears(){
        JsonResult jsonResult = null;
        JsonResultFactory jsonResultFactory = JsonResultFactory.getInstance();
        YearDTO yearDTO = infomationService.getYears();
        jsonResult = jsonResultFactory.buildSuccessResult(yearDTO);
        return jsonResult;
    }


}